package com.hsxc.cdz.controller.consoleController;

import com.hsxc.cdz.base.bean.Address;
import com.hsxc.cdz.base.bean.ConsoleUser;
import com.hsxc.cdz.base.bean.Count;
import com.hsxc.cdz.mgmt.pile.PileManager;
import com.hsxc.cdz.mgmt.station.CountManager;
import com.hsxc.cdz.mgmt.station.StationManager;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 陈钊
 * @version ventor： 支付方式统计、账单统计、电桩状态、 operator： 支付方式统计、账单统计、电桩状态、
 *          opDeviceManager：该运营商下所有电站的电桩状态 pileDeviceManager：某个电站的电桩状态
 *          guest：试用，什么都看不到
 */
@Controller
@RequestMapping("/api")
public class CountController {
    @Autowired
    CountManager countService;
    @Autowired
    private PileManager pileRegister;
    @Autowired
    StationManager stationmanager;
    Logger logger = LoggerFactory.getLogger(CountController.class);

    @RequestMapping(value = "/stationcount", method = RequestMethod.GET)
    @ResponseBody
    public Object getStationCount(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                  HttpSession httpSession) {

        logger.info("START TO COUNT CLASS");
        JSONObject resultJSON = new JSONObject();
        List<Count> counts = new ArrayList<>();
        try {
            httpServletRequest.setCharacterEncoding("UTF-8");
            httpServletResponse.setCharacterEncoding("UTF-8");
            String province = httpServletRequest.getParameter("province");
            String city = httpServletRequest.getParameter("city");

            Address address = new Address();
            address.setProvince(province);
            address.setCity(city);
            logger.info("province" + province);
            logger.info("city" + city);
            resultJSON.put("success", true);
            counts = countService.getCount(address);
            resultJSON.put("data", counts);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return resultJSON;
    }

    // 获取一个充电桩的状态统计信息
    // ==========================================================================================================
    @RequestMapping(value = "/pilecount", method = RequestMethod.GET)
    @ResponseBody
    public Object pileCount(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                            HttpSession httpSession) {
        JSONObject result = new JSONObject();
        List<Count> pileCount = new ArrayList<>();
        // 无权限查看的返回null
        List<Count> pileCountValue = new ArrayList<>();
        ConsoleUser consoleuser = new ConsoleUser();
        consoleuser = (ConsoleUser) httpSession.getAttribute("conuser");
        String operatorNO = consoleuser.getOperatorNO();
        String user_name = consoleuser.getUser_name();
        Subject user = SecurityUtils.getSubject();
        if (user.hasRole("vendor")) {
            pileCount = countService.getPileCount(null, null);
        }
        if (user.hasRole("operator")) {
            pileCount = countService.getPileCount(operatorNO, null);
        }
        if (user.hasRole("stationAdmin")) {
            List<String> stationNOs = stationmanager.getStationNOByName(user_name);
            logger.info("电站编号" + stationNOs);
            pileCount = countService.getPileCount(operatorNO, stationNOs.get(0));
        }
        // guest 试用
        if (user.hasRole("guest")) {
            // 不能看电站状态
            pileCount = null;
        }
        // opDeviceManager运营商设备管理员
        if (user.hasRole("opDeviceManager")) {
            List<String> stationNOs = stationmanager.getStationNOByName(user_name);
            logger.info("电站编号" + stationNOs);
            pileCount = countService.getPileCount(operatorNO, stationNOs.get(0));
        }
        // pileDeviceManager电站设备管理员
        if (user.hasRole("pileDeviceManager")) {
            List<String> stationNOs = stationmanager.getStationNOByName(user_name);
            logger.info("电站编号" + stationNOs);
            pileCount = countService.getPileCount(operatorNO, stationNOs.get(0));
        }
        result.put("success", true);
        if (pileCount == null) {
            result.put("errcode", 1);
            result.put("message", "未找到充电桩");
            return result;
        }
        result.put("errcode", 0);
        for (Count count : pileCount) {
            if (count.getName().equals("1")) {
                count.setName("充电中");
            } else if (count.getName().equals("2")) {
                count.setName("被预约");
            } else if (count.getName().equals("3")) {
                count.setName("故障中");
            } else if (count.getName().equals("4")) {
                count.setName("注销");
            } else if (count.getName().equals("5")) {
                count.setName("未注册");
            } else if (count.getName().equals("6")) {
                count.setName("离线");
            } else if (count.getName().equals("7")) {
                count.setName("被锁定");
            } else if (count.getName().equals("8")) {
                count.setName("待机");
            }
            pileCountValue.add(count);
        }
        result.put("data", pileCountValue);
        return result;
    }

    // 获取一个支付状态统计信息
    // ==========================================================================================================
    @RequestMapping(value = "/paycount", method = RequestMethod.GET)
    @ResponseBody
    public Object paywayCount(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                              HttpSession httpSession) {
        JSONObject result = new JSONObject();
        List<Count> pileCount = new ArrayList<>();
        List<Count> pileCountValue = new ArrayList<>();
        result.put("success", true);
        ConsoleUser consoleuser = new ConsoleUser();
        consoleuser = (ConsoleUser) httpSession.getAttribute("conuser");
        String operatorNO = consoleuser.getOperatorNO();
        String user_name = consoleuser.getUser_name();
        Subject user = SecurityUtils.getSubject();
        if (user.hasRole("vendor")) {
            pileCount = countService.getPaywayCount(operatorNO);
        }
        if (user.hasRole("operator")) {
            pileCount = countService.getPileCount(operatorNO, null);
        }
        if (user.hasRole("stationAdmin")) {
            // 不能看统计信息
            pileCount = null;
        }
        // guest 试用
        if (user.hasRole("guest")) {
            // 不能看统计信息
            pileCount = null;
        }
        // opDeviceManager运营商设备管理员
        if (user.hasRole("opDeviceManager")) {
            // 不能看统计信息
            pileCount = null;
        }
        // pileDeviceManager电站设备管理员
        if (user.hasRole("pileDeviceManager")) {
            // 不能看统计信息
            pileCount = null;
        }
        if (pileCount == null || pileCount.size() < 1) {
            result.put("errcode", 1);
            result.put("message", "没有数据");
            return result;
        }
        result.put("errcode", 0);
        for (Count count : pileCount) {
            if (count.getName().equals("1")) {
                count.setName("微信公众号");
            } else if (count.getName().equals("2")) {
                count.setName("IC卡");
            } else if (count.getName().equals("3")) {
                count.setName("现金");
            } else if (count.getName().equals("4")) {
                count.setName("微信app");
            } else if (count.getName().equals("5")) {
                count.setName("支付宝");
            }
            pileCountValue.add(count);
        }
        result.put("data", pileCountValue);
        return result;
    }

    @RequestMapping(value = "/moneycount", method = RequestMethod.GET)
    @ResponseBody
    public Object moneyCount(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                             HttpSession httpSession) {
        JSONObject result = new JSONObject();
        List<Count> counts = null;
        String userName = null;
        Subject user = SecurityUtils.getSubject();
        if (user.hasRole("vendor")) {
            counts = countService.selectMoneyCount(userName);
        }
        if (user.hasRole("operator")) {
            userName = user.getPrincipal().toString();
            counts = countService.selectMoneyCount(userName);
        }
        if (user.hasRole("stationAdmin") || user.hasRole("guest") || user.hasRole("opDeviceManager") || user.hasRole("pileDeviceManager")) {
            // 不能看统计信息
            counts = null;
        }
        // counts = countService.selectMoneyCount(userName);
        result.put("success", true);
        if (counts == null || counts.size() < 1) {
            result.put("errcode", 1);
            result.put("message", "没有数据");
            return result;
        }

        List<String> date = new ArrayList<>();
        List<Integer> income = new ArrayList<>();
        counts.forEach(x -> {
                    date.add(x.getName());
                    income.add(x.getValue());
                }
        );
        result.put("errcode", 0);
        result.put("date", date);
        result.put("income", income);
        return result;
    }
}